package com.compay4;

import java.sql.*;

public class ShoppingDAO {
    Connection conn = JDBCUtils.getConnection();

    //查找
    public ResultSet find() throws SQLException {
        //获取数据库操作对象
        Statement stmt = conn.createStatement();

        //执行SQL语句。
        String sql = "select * from shopping_info";
        ResultSet rs = stmt.executeQuery(sql);
        return rs;
    }

    //添加
    public void add(String number1, String name1, String price1) throws SQLException {
        //添加一条记录
        String spl = "INSERT INTO shopping_info(number,NAME,price) VALUES(?,?,?)";
        PreparedStatement pstmt = conn.prepareStatement(spl);
        pstmt.setString(1, number1);
        pstmt.setString(2, name1);
        pstmt.setString(3, price1);
        pstmt.executeUpdate();

    }

    //按编号删除
    public void deleteNumber(String number2) throws SQLException {
        //删除一条记录
        String spl = "DELETE FROM shopping_info WHERE number=?";
        PreparedStatement pstmt = conn.prepareStatement(spl);
        pstmt.setString(1, number2);
        pstmt.executeUpdate();
    }

    //按名称删除
    public void deleteName(String name2) throws SQLException {
        //删除一条记录
        String spl = "DELETE FROM shopping_info WHERE name=?";
        PreparedStatement pstmt = conn.prepareStatement(spl);
        pstmt.setString(1, name2);
        pstmt.executeUpdate();
    }

    //按编号修改
    public void correctNumber(String price2, String number) throws SQLException {
        String spl = "UPDATE shopping_info SET price=? WHERE number=?";
        PreparedStatement pstmt = conn.prepareStatement(spl);
        pstmt.setString(1, price2);
        pstmt.setString(2, number);
        pstmt.executeUpdate();

    }

    //按名称修改
    public void correctName(String price3, String name) throws SQLException {
        String spl = "UPDATE shopping_info SET price=? WHERE name=?";
        PreparedStatement pstmt = conn.prepareStatement(spl);
        pstmt.setString(1, price3);
        pstmt.setString(2, name);
        pstmt.executeUpdate();

    }
}
